package com.tyjc.unique.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.tyjc.lottery.R;


public class CustomProgress extends Dialog {
	 public CustomProgress(Context context) {
		    super(context);
		  }

		  public CustomProgress(Context context, int theme) {
		    super(context, theme);
		  }

//		  /**
//		   * 当窗口焦点改变时调用
//		   */
//		  public void onWindowFocusChanged(boolean hasFocus) {
//		    ImageView imageView = (ImageView) findViewById(R.id.iv_spinnerImageView);
//		    // 动画
//			Animation loadAnimation = AnimationUtils.loadAnimation(
//					context, R.anim.load_animation);
//			// 菊花旋转动画
//			imageView.startAnimation(loadAnimation);
//		  }

		  /**
		   * 给Dialog设置提示信息
		   * 
		   * @param message
		   */
		  public void setMessage(CharSequence message) {
		    if (message != null && message.length() > 0) {
		      findViewById(R.id.tv_message).setVisibility(View.VISIBLE);
		      TextView txt = (TextView) findViewById(R.id.tv_message);
		      txt.setText(message);
		      txt.invalidate();
		    }
		  }

		  /**
		   * 弹出自定义ProgressDialog
		   * 
		   * @param context
		   *            上下文
		   * @param message
		   *            提示
		   * @param cancelable
		   *            是否按返回键取消
		   * @param cancelListener
		   *            按下返回键监听
		   * @return
		   */
		  public static CustomProgress show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) {
		    CustomProgress dialog = new CustomProgress(context, R.style.Custom_Progress);
		    dialog.setTitle("");
		    dialog.setContentView(R.layout.dg_progress_custom);
		    if (message == null || message.length() == 0) {
		      dialog.findViewById(R.id.tv_message).setVisibility(View.GONE);
		    } else {
		      TextView txt = (TextView) dialog.findViewById(R.id.tv_message);
		      txt.setText(message);
		    }
		    ImageView imageView = (ImageView) dialog.findViewById(R.id.iv_spinnerImageView);
		    // 动画
			Animation loadAnimation = AnimationUtils.loadAnimation(
					context, R.anim.load_animation);
			// 菊花旋转动画
			imageView.startAnimation(loadAnimation);
		    
		    // 按返回键是否取消
		    dialog.setCancelable(cancelable);
		    // 监听返回键处理
		    dialog.setOnCancelListener(cancelListener);
		    // 设置居中
		    dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
		    // 设置背景层透明度
		    lp.dimAmount = 0.2f;
		    dialog.getWindow().setAttributes(lp);
		    // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		    dialog.show();
		    return dialog;
		  }
}

